<template>
	<view class="wrap yjfk-form-right">
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">问题反馈</block>
		</cu-custom>
		<u-form class="form" customeStyle="padding:20rpx 30rpx" :model="model" :rules="rules" ref="uForm"
			labelPosition="top">
			<u-form-item label="问题和意见" labelWidth="100vh" prop="content">
				<u--textarea placeholder="请填写10个字以上的问题描述以便我们提供更好的帮助" v-model="model.content" maxlength="500"
					count></u--textarea>
			</u-form-item>
			<u-form-item label="图片(选填,提供问题截图,单张图片大小20M一下)" labelWidth="100vh" prop="content">
				<u-upload :fileList="model.fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="10" uploadIcon="plus" uploadText="选择图片">
				</u-upload>
			</u-form-item>
			<u-form-item label="反馈类型" labelWidth="100vh" prop="contact">
				<u-radio-group v-model="model.type" placement="row">
					<u-radio activeColor="red" label="新需求" name="newidea"></u-radio>
					<u-radio activeColor="red" label="bug" name="bug"></u-radio>
					<u-radio activeColor="red" label="建议" name="advice"></u-radio>
				</u-radio-group>
			</u-form-item>


		</u-form>
		<view class="form-footer">
			<u-button class="btn" type="primary" @click="submit">提交</u-button>
			<!-- <u-button class="btn" type="default" @click="cancel">关闭</u-button> -->
		</view>
	</view>
</template>
<script>
	/**
	 * Copyright (c) 2013-Now http://aidex.vip All rights reserved.
	 */
	export default {
		data() {
			return {
				model: {
					id: '',
					category: '',
					content: '',
					contact: '',
					deviceInfo: '',
					type: '',
					fileList1: [{
						url: 'https://img0.baidu.com/it/u=3846695387,1572281966&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750'
					}, {
						url: 'https://gd-hbimg.huaban.com/32c03991a4b160f7995939e3cbb15635211a0d503e132-YttxNL_fw658'
					}, {
						url: 'https://img2.baidu.com/it/u=2819655934,963197807&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=708'
					}, {
						url: 'https://img2.baidu.com/it/u=2037909377,464167036&fm=253&fmt=auto&app=138&f=JPEG?w=394&h=499'
					}],
				},
				rules: {
					category: [{
						required: true,
						message: '请选择问题和意见的分类',
						trigger: ['change', 'blur'],
					}],
					content: [{
						required: true,
						min: 10,
						max: 500,
						message: '问题和意见在 10 到 500 个字符之间',
						trigger: ['change', 'blur'],
					}],
				}
			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
			// 获取设备信息
			uni.getSystemInfo({
				success: res => {
					this.model.deviceInfo = JSON.stringify(res);
				}
			});
		},
		options: {
			styleIsolation: 'shared'
		},
		methods: {
			// 删除图片
			deletePic(event) {

				this.model[`fileList${event.name}`].splice(event.index, 1)
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.model[`fileList${event.name}`].length
				lists.map((item) => {
					this.model[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.model[`fileList${event.name}`][fileListLen]
					this.model[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			submit() {
				// console.log(this.model)
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.$u.api.commentSave(this.model).then(res => {
							uni.showModal({
								title: '提示',
								content: res.message,
								showCancel: false,
								success: function() {
									if (res.result == 'true') {
										uni.navigateBack();
									}
								}
							});
						});
					} else {
						this.$u.toast('您填写的信息有误，请根据提示修正。');
					}
				});
			},
			cancel() {
				uni.navigateBack();
			}
		}
	};
</script>
<style lang="scss" scoped>
	.input-placeholder {
		text-align: right;
	}

	:deep .u-form {
		display: flex;
		flex-direction: column;
		padding: 20rpx 30rpx;

	}
</style>